<?php
require_once("./define/define_table.php");

define("ADD_LIEU_FORM", "Formulaire d'ajout d'un lieu");
define("MODIFY_LIEU_FORM", "Formulaire de modification d'un lieu");

class Lieu{
	
	private $id_lieu;
	private $nom;
	private $description;
	private $numero_rue;
	private $rue;
	private $code_postal;
	private $ville;
	
	public function Lieu(){
	       
	}
	
	public function getIdLieu(){
		return $this->id_lieu;
	}
	
	public function getNom(){
		return $this->nom;
	}
	
	public function getDescription(){
		return $this->description;
	}
	
	public function getNumeroRue(){
		return $this->numero_rue;
	}
	
	public function getRue(){
		return $this->rue;
	}
	
	public function getCodePostal(){
		return $this->code_postal;
	}
	
	public function getVille(){
		return $this->ville;
	}
	
	public function setIdLieu($id){
	    $this->id_lieu = $id;
	}
	
	public function setNom($nom){
	    $this->nom = $nom;
	}
	
	public function setDescription($description){
	    $this->description = $description;
	}
	
	public function setNumeroRue($numero){
	    $this->numero_rue = $numero;
	}
	
	public function setRue($rue){
	    $this->rue = $rue;
	}
	
	public function setCodePostal($code){
	    $this->code_postal = $code;
	}
	
	public function setVille($ville){
	    $this->ville = $ville;
	}
	
	public function getAllLieu(){
	    global $db;
		try{
			$sql = "SELECT * FROM ".LIEU_TABLE;
			$sth = $db->prepare($sql);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Lieu");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public function getLieu($id){
	    global $db;
	    try{
	        $sql = "SELECT * FROM ".LIEU_TABLE." WHERE id_lieu = :id_lieu";
	        $sth = $db->prepare($sql);
	        $sth->bindValue(":id_lieu", $id, PDO::PARAM_INT);
	        $sth->execute();
	        return $sth->fetchObject("Lieu");
	    }catch(PDOException $e){
	        echo $e.getMessage();
	    }
	}
	
	public function addOrModify(){
		global $db;
		try{
			if($this->id_lieu == null){
				$sql = "INSERT INTO ".LIEU_TABLE." VALUES(null, :nom, :description, :numero_rue, :rue, :code_postal, :ville)";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
				$sth->bindValue(":description", $this->description, PDO::PARAM_STR);
				$sth->bindValue(":numero_rue", $this->numero_rue, PDO::PARAM_INT);
				$sth->bindValue(":rue", $this->rue, PDO::PARAM_STR);
				$sth->bindValue(":code_postal", $this->code_postal, PDO::PARAM_INT);
				$sth->bindValue(":ville", $this->ville, PDO::PARAM_STR);
			}
			else{
				$sql = "UPDATE ".LIEU_TABLE." SET nom = :nom, description = :description, numero_rue = :numero_rue, rue = :rue,
								code_postal = :code_postal, ville = :ville WHERE id_lieu= :id_lieu";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
				$sth->bindValue(":description", $this->description, PDO::PARAM_STR);
				$sth->bindValue(":numero_rue", $this->numero_rue, PDO::PARAM_INT);
				$sth->bindValue(":rue", $this->rue, PDO::PARAM_STR);
				$sth->bindValue(":code_postal", $this->code_postal, PDO::PARAM_INT);
				$sth->bindValue(":ville", $this->ville, PDO::PARAM_STR);
				$sth->bindValue(":id_lieu", $this->id_lieu, PDO::PARAM_INT);
			}
			PrintQuery::queryPrint($sth);
			return $sth->execute(); 
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public static function getAddForm(){
		$html = ADD_LIEU_FORM .
				'<form class="ajax_form" id="add_lieu_form" action="?module=lieu&action=doAdd" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._NOM_LIEU_.'</td>' .
				'			<td><input type="text" name="nom" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._DESCRIPTION_.'</td>' .
				'			<td><input type="text" name="description" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._NUMERO_RUE_.'</td>' .
				'			<td><input type="text" name="numero_rue" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._RUE_.'</td>' .
				'			<td><input type="text" name="rue" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._CODE_POSTAL_.'</td>' .
				'			<td><input type="text" name="code_postal" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._VILLE_.'</td>' .
				'			<td><input type="text" name="ville" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function getModifyForm(){
		$html = MODIFY_LIEU_FORM .
				'<form class="ajax_form" id="modify_lieu_form" action="?module=lieu&action=doModify" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._ID_LIEU_.'</td>' .
				'			<td><input readonly type="text" name="id_lieu" value="'.$this->id_lieu.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._NOM_LIEU_.'</td>' .
				'			<td><input type="text" name="nom" value="'.$this->nom.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._DESCRIPTION_.'</td>' .
				'			<td><input type="text" name="description" value = "'.$this->description.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._NUMERO_RUE_.'</td>' .
				'			<td><input type="text" name="numero_rue" value = "'.$this->numero_rue.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._RUE_.'</td>' .
				'			<td><input type="text" name="rue" value = "'.$this->rue.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._CODE_POSTAL_.'</td>' .
				'			<td><input type="text" name="code_postal" value = "'.$this->code_postal.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._VILLE_.'</td>' .
				'			<td><input type="text" name="ville" value = "'.$this->ville.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}

	public function delete(){
	    global $db;
	    $sql = "DELETE FROM ".LIEU_TABLE." WHERE id_lieu = :id_lieu";
	    $sth = $db->prepare($sql);
	    $sth->bindValue(":id_lieu", $this->id_lieu, PDO::PARAM_INT);
	    $sth->execute();
	    PrintQuery::queryPrint($sth);
	}
	
	public function generateSelect($id = null){
	    $array_evenement= self::getAllLieu();
	    $html = '<select name="id_lieu">';
	    foreach($array_evenement as $l){
	        $selected = ($l->getIdLieu() == $id) ?  'selected="selected"' : '';
	        $html .= '<option value="'.$l->getIdLieu().'" '.$selected.'>'.$l->getNom().'</option>';
	    }
	    $html .= '</select>';
	    return $html;
	}
}
?>
